package com.ytjj.qmyx.admin.service.impl;

import com.github.pagehelper.PageHelper;
import com.ytjj.common.api.CommonPage;
import com.ytjj.qmyx.admin.dao.LoginLogDao;
import com.ytjj.qmyx.admin.dao.PayLogDao;
import com.ytjj.qmyx.admin.mapper.AdminErrorLogMapper;
import com.ytjj.qmyx.admin.mapper.AdminLogMapper;
import com.ytjj.qmyx.admin.model.AdminErrorLog;
import com.ytjj.qmyx.admin.model.AdminErrorLogExample;
import com.ytjj.qmyx.admin.model.AdminLog;
import com.ytjj.qmyx.admin.model.AdminLogExample;
import com.ytjj.qmyx.admin.model.request.LoginLogRequest;
import com.ytjj.qmyx.admin.model.request.OperationErrorLogRequest;
import com.ytjj.qmyx.admin.model.request.OperationLogRequest;
import com.ytjj.qmyx.admin.model.request.PayLogRequest;
import com.ytjj.qmyx.admin.model.response.OrdersPayResponse;
import com.ytjj.qmyx.admin.service.LogService;
import com.ytjj.qmyx.mall.model.LoginLog;
import com.ytjj.qmyx.mall.model.OrdersPay;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @Description 日志记录
 *
 * @Version 1.0
 * 
 */

@Service
public class LogServiceImpl implements LogService {

    @Resource
    private PayLogDao payLogDao;
    @Resource
    private LoginLogDao loginLogDao;
    @Resource
    private AdminLogMapper adminLogMapper;
    @Resource
    private AdminErrorLogMapper adminErrorLogMapper;

    @Override
    public CommonPage<OrdersPayResponse> getPayLog(PayLogRequest payLogRequest) {
        PageHelper.startPage(payLogRequest.getPageNum(), payLogRequest.getPageSize());
        List<OrdersPayResponse> ordersPayList = Optional.ofNullable(payLogDao.getPayLogList(payLogRequest))
                .orElse(new ArrayList<>());
        return CommonPage.restPage(ordersPayList);
    }

    @Override
    public CommonPage<LoginLog> getLoginLogList(LoginLogRequest loginLogRequest) {
        PageHelper.startPage(loginLogRequest.getPageNum(), loginLogRequest.getPageSize());
        List<LoginLog> loginLogList = Optional.ofNullable(loginLogDao.getLoginLogList(loginLogRequest))
                .orElse(new ArrayList<>());
        return CommonPage.restPage(loginLogList);
    }

    @Override
    public CommonPage<AdminLog> getOperLogList(OperationLogRequest operationLogRequest) {
        PageHelper.startPage(operationLogRequest.getPageNum(), operationLogRequest.getPageSize());
        AdminLogExample adminLogExample = new AdminLogExample();
        AdminLogExample.Criteria criteria = adminLogExample.createCriteria();
        if (operationLogRequest.getOperDesc() != null && operationLogRequest.getOperDesc().length() > 0) {
            criteria.andOperDescEqualTo(operationLogRequest.getOperDesc());
        }
        if (!StringUtils.isEmpty(operationLogRequest.getOperModule())) {
            criteria.andOperModuleEqualTo(operationLogRequest.getOperModule());
        }
        List<AdminLog> adminLogs;
        if (null!= operationLogRequest.getProductId()){
            adminLogs = adminLogMapper.selectByProductId(operationLogRequest.getProductId());
        }else {
            adminLogs = adminLogMapper.selectByExampleWithBLOBs(adminLogExample);

        }
        List<AdminLog> adminLogList = Optional.ofNullable(adminLogs).orElse(new ArrayList<>());

        return CommonPage.restPage(adminLogList);
    }

    @Override
    public CommonPage<AdminErrorLog> getOperErrorLogList(OperationErrorLogRequest operationErrorLogRequest) {
        PageHelper.startPage(operationErrorLogRequest.getPageNum(), operationErrorLogRequest.getPageSize());
        AdminErrorLogExample adminErrorLogExample = new AdminErrorLogExample();
        adminErrorLogExample.setOrderByClause("create_time DESC");
        List<AdminErrorLog> adminErrorLogList = Optional.ofNullable(adminErrorLogMapper.selectByExampleWithBLOBs(adminErrorLogExample))
                .orElse(new ArrayList<>());
        return CommonPage.restPage(adminErrorLogList);
    }

}
